﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MaxZhang.EasyEntities.Attribute;

namespace MaxZhang.EasyEntities.Dynamic.Aop
{
    /// <summary>
    /// AOP方法信息
    /// </summary>
    public class AopMethodInfo
    {
        private string _name;

        private List<AopAspectInfo> _aspectList = new List<AopAspectInfo>(3);

        public AopMethodInfo(string name)
        {
            _name = name;
        }


        public string Name
        {
            set { _name = value; }
            get { return _name; }
        }


        public List<AopAspectInfo> AspectList
        {
            get
            {
                return _aspectList;
            }
        }

        /// <summary>
        /// 添加切面信息
        /// </summary>
        /// <param name="id">切面ID</param>
        /// <param name="atype">切面类型</param>
        /// <returns>是否添加成功</returns>
        public bool AddAspectInfo(string id, InterceptorBaseAttribute atype)
        {
            var item = GetAspectInfoById(id);
            
            if (item != null)
                return false;
            
            _aspectList.Add(new AopAspectInfo(id, atype));
            return true;
        }


        /// <summary>
        /// 删除切面信息
        /// </summary>
        /// <param name="id">切面信息ID</param>
        /// <returns>是否成功删除</returns>
        public bool RemoveAspectInfo(string id)
        {
            var item = GetAspectInfoById(id);

            if (item == null)
                return false;

            return _aspectList.Remove(item);
        }


        /// <summary>
        /// 根据ID得到切面信息
        /// </summary>
        /// <param name="id">切面信息id</param>
        /// <returns>切面信息</returns>
        public AopAspectInfo GetAspectInfoById(string id)
        {
            var item = (from a in _aspectList
                        where a.Id.Equals(id)
                        select a).FirstOrDefault();
            
            return item;
        }

        /// <summary>
        /// 得到此方法信息中的所有切面信息
        /// </summary>
        /// <returns>切面信息列表</returns>
        public List<AopAspectInfo> GetAllGetAspectInfo()
        {
            return this._aspectList;
        }


    }
}
